﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace SketchUpAPI.NET.Objects
{
    public class Group : ComponentInstance
    {
        #region API 函数
        /// <summary>
        /// 创建Group实例
        /// </summary>
        /// <param name="group"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGroupCreate(out IntPtr group);

        /// <summary>
        /// 设置群组的名字
        /// </summary>
        /// <param name="group"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGroupSetName(IntPtr group, [MarshalAs(UnmanagedType.LPUTF8Str)] string name);

        /// <summary>
        /// 获取群组的名字
        /// </summary>
        /// <param name="group"></param>
        /// <param name="name"><see cref="StringRef"/></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGroupGetName(IntPtr group, ref IntPtr name);

        /// <summary>
        /// 获取群组Guid
        /// </summary>
        /// <param name="group"></param>
        /// <param name="guid"><see cref="StringRef"/></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGroupGetGuid(IntPtr group, ref IntPtr guid);

        /// <summary>
        /// 设置Guid
        /// </summary>
        /// <param name="group"></param>
        /// <param name="guid_str"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGroupSetGuid(IntPtr group, [MarshalAs(UnmanagedType.LPUTF8Str)] string guid_str);

        /// <summary>
        /// 设置变换
        /// </summary>
        /// <param name="group"></param>
        /// <param name="transform"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGroupSetTransform(IntPtr group, Transformation transform);

        /// <summary>
        /// 获取变换
        /// </summary>
        /// <param name="group"></param>
        /// <param name="transform"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGroupGetTransform(IntPtr group, ref Transformation transform);

        /// <summary>
        /// 获取群组的物件表
        /// </summary>
        /// <param name="group"></param>
        /// <param name="entities"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGroupGetEntities(IntPtr group, out IntPtr entities);

        /// <summary>
        /// 获取群组定义
        /// </summary>
        /// <param name="group"></param>
        /// <param name="component"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGroupGetDefinition(IntPtr group, out IntPtr component);
        #endregion

        /// <summary>
        /// 群组名称
        /// </summary>
        public override string Name
        {
            get
            {
                StringRef s = StringRef.Empty;
                SketchUpExceptionHelper.ThrowSUResult(SUGroupGetName(Id, ref s.Id));
                return s.Dispose();
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUGroupSetName(Id, value));
            }
        }

        /// <summary>
        /// Guid
        /// </summary>
        public override string Guid
        {
            get
            {
                StringRef s = StringRef.Empty;
                SketchUpExceptionHelper.ThrowSUResult(SUGroupGetGuid(Id, ref s.Id));
                return s.Dispose();
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUGroupSetGuid(Id, value));
            }
        }

        /// <summary>
        /// 群组的物件表
        /// </summary>
        public EntityTable Entities
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUGroupGetEntities(Id, out IntPtr id));
                return new EntityTable(id);
            }
        }

        /// <summary>
        /// 变换
        /// </summary>
        public override Transformation Transform
        {
            get
            {
                Transformation t = new Transformation();
                SketchUpExceptionHelper.ThrowSUResult(SUGroupGetTransform(Id, ref t));
                return t;
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUGroupSetTransform(Id, value));
            }
        }

        /// <summary>
        /// 群组定义
        /// </summary>
        public override ComponentDefinition Definition
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUGroupGetDefinition(Id, out IntPtr id));
                return new ComponentDefinition(id);
            }
        }

        /// <summary>
        /// 创建群组
        /// </summary>
        public Group()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUGroupCreate(out Id));
        }

        internal Group(IntPtr id) : base(id) { }
    }
}